<?php
//XML output of an existing MySql database
header("Content-type: text/xml");

require('conn.php');

//to select the database here test is the sample database come with mysql
$db = mysql_select_db("devexDB",$connection)
or die ("Couldn't select database.");

// Build query.
$query = "SELECT DISTINCT peaks.id, peaks.name, peaks.latitude, peaks.longitude, peaks.altitude FROM peaks, lists_peaks, lists WHERE true";
$country = $_GET['country'];
$parent = $_GET['parent'];
$min_altitude = $_GET['min_altitude'];
$max_altitude = $_GET['max_altitude'];
$min_prominence = $_GET['min_prominence'];
$max_prominence = $_GET['max_prominence'];
$geo_bounds = $_GET['geo_bounds']; // In format LAT:LNG-LAT:LNG
$list = $_GET['list'];

if ($country) $query = $query." AND peaks.country = \"$country\"";
if ($min_altitude) $query = $query." AND peaks.altitude >= \"$min_altitude\"";
if ($max_altitude) $query = $query." AND peaks.altitude <= \"$max_altitude\"";
if ($min_prominence) $query = $query." AND peaks.prominence >= \"$min_prominence\"";
if ($max_prominence) $query = $query." AND peaks.prominence <= \"$max_prominence\"";
if ($parent) $query = $query." AND peaks.parent = \"$parent\"";
if ($geo_bounds) {
  $bounds = explode("~", $geo_bounds);
  $sw = explode(":", $bounds[0]);
  $ne = explode(":", $bounds[1]);
  $query = $query." AND peaks.latitude >= $sw[0] AND peaks.latitude <= $ne[0]";
  $query = $query." AND peaks.longitude >= $sw[1] AND peaks.longitude <= $ne[1]";
}
if ($list && $list != "") {
  $query = $query." AND peaks.id = lists_peaks.peak_id AND lists_peaks.list_id = lists.id AND lists.name = \"$list\"";
}

$rs = mysql_query($query,$connection)
or die ("invalid query");

//count the no. of  columns in the table
$fcount = mysql_num_fields($rs);

//you can choose any name for the starting tag
echo ("<peaks>");

while($row = mysql_fetch_array( $rs ) ) {
  echo ("<peak>");
  for($i=0; $i< $fcount; $i++) {
    $tag = mysql_field_name( $rs, $i );
    echo ("<$tag>". $row[$i]. "</$tag>");
  }
  echo ("</peak>");
}
echo ("</peaks>");
?>